/**
 * 
 */
package com.android.dwt.ui;


import java.util.ArrayList;

import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;

import com.android.dwt.R;
import com.android.dwt.data.GTDApp;
import com.android.dwt.fragment.ContextFrag;
import com.android.dwt.fragment.ListFrag;
import com.android.dwt.fragment.LocationFrag;
import com.android.dwt.fragment.MoreFrag;
import com.android.dwt.fragment.TodayFrag;
import com.android.dwt.service.DWTService;
import com.android.dwt.service.DWTService.MyBinder;
import com.android.dwt.util.C;

/**
 * @author xiang
 *
 */
public class MainAct_old extends TabActivity
		implements OnCheckedChangeListener {
	
	private final String TAG = "MainAct";
	public static final int bgColor = 0xff444444;
	public static final int hlColor = 0xff666666;
	private static final int SWIPE_MIN_DISTANCE = 100;
	private static final int SWIPE_MAX_OFF_PATH = 250;
	private static final int SWIPE_THRESHOLD_VELOCITY = 180;
	
	private TabHost mTabHost;
	private GTDApp mGa;
	private GestureDetector mDetector;
	private int mCurrentView = 0;
	private static int mMaxTabIndex = 4;
	private ArrayList<RadioButton> mButtonList;
	private ArrayList<View> mViews;
	
	private Intent ittList;
	private Intent ittToday;
	private Intent ittContext;
	private Intent ittPosition;
	private Intent ittMore;
	
	private RadioButton rbList;
	private RadioButton rbToday;
	private RadioButton rbContext;
	private RadioButton rbPosition;
	private RadioButton rbMore;
	
	private View vList;
	private View vToday;
	private View vContext;
	private View vPosition;
	private View vMore;
//	private View curTitleView;
	
	
	public class MyGestureDetectorListener extends SimpleOnGestureListener {
		
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			
//			return super.onFling(e1, e2, velocityX, velocityY);
			
			if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
				
				return true;
			}
			
			// right to left swipe
			if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				
				Log.i(TAG, "right to left swipe");
				
//				mButtonList.get(mCurrentView).setBackgroundColor(bgColor);
				mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bar_top);
				mButtonList.get(mCurrentView).setChecked(false);
				mViews.get(mCurrentView).setVisibility(View.INVISIBLE);
				
				if (mCurrentView == mMaxTabIndex) {
					
					mCurrentView = 0;
					
				} else {
					
					mCurrentView++;
				}
				
				mTabHost.setCurrentTab(mCurrentView);
				
//				mButtonList.get(mCurrentView).setBackgroundColor(hlColor);
				mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bg_hl);
				mButtonList.get(mCurrentView).setChecked(true);
				mViews.get(mCurrentView).setVisibility(View.VISIBLE);
				
			} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				
				Log.e(TAG, "left to right swipe");
				
//				mButtonList.get(mCurrentView).setBackgroundColor(bgColor);
				mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bar_top);
				mButtonList.get(mCurrentView).setChecked(false);
				mViews.get(mCurrentView).setVisibility(View.INVISIBLE);
				
				if (mCurrentView == 0) {
					
					mCurrentView = mMaxTabIndex;
					
				} else {
					
					mCurrentView--;
				}
				
				mTabHost.setCurrentTab(mCurrentView);
				
//				mButtonList.get(mCurrentView).setBackgroundColor(hlColor);
				mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bg_hl);
				mButtonList.get(mCurrentView).setChecked(true);
				mViews.get(mCurrentView).setVisibility(View.VISIBLE);
			}
				
			return true;		// has consumed this event
		}
		
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		super.onCreate(savedInstanceState);
		
		Log.e(TAG, "onCreate is called!");
		
//		useTitleFeature = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);		// true
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.main);
		
		initTitleView();
		
		mGa = (GTDApp) getApplication();
		mDetector = new GestureDetector(this, new MyGestureDetectorListener());
		
		// connect to service
		bindService(new Intent(MainAct_old.this, DWTService.class),
				new ServiceConnection() {
			
			@Override
			public void onServiceDisconnected(ComponentName name) {
				
				
			}
			
			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				
				MyBinder binder = (MyBinder) service;
				DWTService svc = binder.getService();
				
				svc.checkGPS(MainAct_old.this);
				
				unbindService(this);
			}
			
		},
		BIND_AUTO_CREATE);
		
		this.ittList = new Intent(this, ListFrag.class);
		
		this.ittToday = new Intent(this, TodayFrag.class);
		this.ittToday.putExtra("today", true);
		
		this.ittContext = new Intent(this, ContextFrag.class);
		this.ittPosition = new Intent(this, LocationFrag.class);
		this.ittMore = new Intent(this, MoreFrag.class);
		
		rbList = (RadioButton) findViewById(R.id.rb_list);
		rbToday = (RadioButton) findViewById(R.id.rb_today);
		rbContext = (RadioButton) findViewById(R.id.rb_context);
		rbPosition = (RadioButton) findViewById(R.id.rb_position);
		rbMore = (RadioButton) findViewById(R.id.rb_more);
		
		mButtonList = new ArrayList<RadioButton>();
		mButtonList.add(rbList);
		mButtonList.add(rbToday);
		mButtonList.add(rbContext);
		mButtonList.add(rbPosition);
		mButtonList.add(rbMore);
		
		mViews = new ArrayList<View>();
		mViews.add(vList);
		mViews.add(vToday);
		mViews.add(vContext);
		mViews.add(vPosition);
		mViews.add(vMore);
		
		initRadios();
		
		setupIntent();
		
		mTabHost.setLongClickable(true);
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		
		Log.e(TAG, "onStart is called!");
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
		Log.e(TAG, "onStop is called!");
	}
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		
		Log.e(TAG, "onRestart is called!");
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		Log.e(TAG, "onResume is called!");
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		Log.e(TAG, "onPause is called!");
		
//		handleSearchQuery(getIntent());		// when press search button, the original activity is paused.
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		Log.e(TAG, "onDestroy is called!");
	}
	
	
	private void initTitleView() {
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_main);
		vList = findViewById(R.id.ll_main);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_today);
		vToday = findViewById(R.id.ll_today);
		vToday.setVisibility(View.GONE);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_context);
		vContext = findViewById(R.id.ll_context);
		vContext.setVisibility(View.GONE);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_pos);
		vPosition = findViewById(R.id.ll_pos);
		vPosition.setVisibility(View.GONE);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_more);
		vMore = findViewById(R.id.ll_more);
		vMore.setVisibility(View.GONE);
	}
	
	
	private void initRadios() {
		
		rbList.setOnCheckedChangeListener(this);
		rbToday.setOnCheckedChangeListener(this);
		rbContext.setOnCheckedChangeListener(this);
		rbPosition.setOnCheckedChangeListener(this);
		rbMore.setOnCheckedChangeListener(this);
		
//		rbCurrent = rbList;
//		rbCurrent.setBackgroundColor(hlColor);
		
//		mButtonList.get(0).setBackgroundColor(hlColor);
		mButtonList.get(0).setBackgroundResource(R.drawable.bg_hl);
	}
	
	private void setupIntent() {
		
		this.mTabHost = getTabHost(); // The activity TabHost

		mTabHost.addTab(mTabHost.newTabSpec("list")
				.setIndicator(getString(R.string.panel_list), null)
				.setContent(ittList));
		
		mTabHost.addTab(mTabHost.newTabSpec("today")
				.setIndicator(getString(R.string.panel_today), null)
				.setContent(ittToday));
		
		mTabHost.addTab(mTabHost.newTabSpec("context")
				.setIndicator(getString(R.string.panel_context), null)
				.setContent(ittContext));
		
		mTabHost.addTab(mTabHost.newTabSpec("position")
				.setIndicator(getString(R.string.panel_pos), null)
				.setContent(ittPosition));
		
		mTabHost.addTab(mTabHost.newTabSpec("more")
				.setIndicator(getString(R.string.panel_more), null)
				.setContent(ittMore));
		
		mTabHost.setCurrentTab(0);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
//		return super.onCreateOptionsMenu(menu);
		
		boolean result = super.onCreateOptionsMenu(menu);
		
		menu.add(0, C.MENU.NEW_TASK, Menu.NONE, getString(R.string.menu_new_task))
			.setIcon(android.R.drawable.ic_menu_add);
		
		/*menu.add(0, C.MENU.COMPLETED, Menu.NONE, getString(R.string.menu_completed))
			.setIcon(android.R.drawable.ic_dialog_info);
		
		menu.add(0, C.MENU.TRASH, Menu.NONE, getString(R.string.menu_trash))
			.setIcon(android.R.drawable.btn_star);*/
		
		menu.add(0, C.MENU.SEARCH, Menu.NONE, getString(R.string.menu_search)).setIcon(
				android.R.drawable.ic_menu_search);
		
		menu.add(0, C.MENU.SETTINGS, Menu.NONE, getString(R.string.menu_settings)).setIcon(
				R.drawable.menu_setting);
		
		return result;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		
		Intent itt = null;
		// int index = myListView.getSelectedItemPosition();
		
		switch (item.getItemId()) {
		
			case C.MENU.NEW_TASK:
				
				itt = new Intent(MainAct_old.this, NewTaskAct.class);
				
				itt.putExtra("list_index", 0);
				itt.putExtra("new_task", true);
				
//				startActivityForResult(itt, 0);
				startActivity(itt);
				
				return true;
				
			/*case C.MENU.COMPLETED: 		// completed task
				
				// setTitle("edit list");
				// Cursor csr =
				// ga.getDbta().fetchTasks(DBTaskAdapter.KEY_COMPLETED+"="+TaskItem.COMPLETED);
				itt = new Intent(MainAct.this, ViewTaskAct.class);
	
				itt.putExtra("list_index", -1);
				itt.putExtra("task_status", C.TASK.COMPLETED);
	
				startActivity(itt);
	
				return true;
	
			case C.MENU.TRASH: // deleted task
				// setTitle("remove list");
				itt = new Intent(MainAct.this, ViewTaskAct.class);
	
				itt.putExtra("list_index", -1);
				itt.putExtra("task_status", C.TASK.CANCELLED);
	
				startActivity(itt);
	
				return true;*/
				
			case C.MENU.SEARCH:
				
				onSearchRequested();
				
//				itt = getIntent();
//				itt.setClass(MainAct.this, SearchAct.class);
//				itt = new Intent(MainAct.this, SearchAct.class);
//				
//				startActivity(itt);
				return true;
				
			case C.MENU.SETTINGS:
				
				itt = new Intent(MainAct_old.this, SettingAct.class);
    			startActivity(itt);
	
			default:
				break;
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		
		if (isChecked) {
			
			switch (buttonView.getId()) {
			
				case R.id.rb_list:
					
					this.mTabHost.setCurrentTabByTag("list");
					
//					mButtonList.get(mCurrentView).setBackgroundColor(bgColor);
					mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bar_top);
					mViews.get(mCurrentView).setVisibility(View.INVISIBLE);
					
					mCurrentView = 0;
					
//					mButtonList.get(mCurrentView).setBackgroundColor(hlColor);
					mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bg_hl);
					mViews.get(mCurrentView).setVisibility(View.VISIBLE);
					
					break;
					
				case R.id.rb_today:
					
					this.mTabHost.setCurrentTabByTag("today");
					
//					mButtonList.get(mCurrentView).setBackgroundColor(bgColor);
					mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bar_top);
					mViews.get(mCurrentView).setVisibility(View.INVISIBLE);
					
					mCurrentView = 1;
					
//					mButtonList.get(mCurrentView).setBackgroundColor(hlColor);
					mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bg_hl);
					mViews.get(mCurrentView).setVisibility(View.VISIBLE);
					
					/*rbToday.setBackgroundColor(hlColor);
					rbCurrent.setBackgroundColor(bgColor);
					rbCurrent = rbToday;*/
					
					/*curTitleView.setVisibility(View.INVISIBLE);
					vToday.setVisibility(View.VISIBLE);
					curTitleView = vToday;*/
					
					break;
					
				case R.id.rb_context:
					
					this.mTabHost.setCurrentTabByTag("context");
					
//					mButtonList.get(mCurrentView).setBackgroundColor(bgColor);
					mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bar_top);
					mViews.get(mCurrentView).setVisibility(View.INVISIBLE);
					
					mCurrentView = 2;
					
//					mButtonList.get(mCurrentView).setBackgroundColor(hlColor);
					mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bg_hl);
					mViews.get(mCurrentView).setVisibility(View.VISIBLE);
					
					/*rbContext.setBackgroundColor(hlColor);
					rbCurrent.setBackgroundColor(bgColor);
					rbCurrent = rbContext;*/
					
					/*curTitleView.setVisibility(View.INVISIBLE);
					vContext.setVisibility(View.VISIBLE);
					curTitleView = vContext;*/
					
					break;
					
				case R.id.rb_position:
					
					this.mTabHost.setCurrentTabByTag("position");
					
//					mButtonList.get(mCurrentView).setBackgroundColor(bgColor);
					mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bar_top);
					mViews.get(mCurrentView).setVisibility(View.INVISIBLE);
					
					mCurrentView = 3;
					
//					mButtonList.get(mCurrentView).setBackgroundColor(hlColor);
					mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bg_hl);
					mViews.get(mCurrentView).setVisibility(View.VISIBLE);
					
					/*rbPosition.setBackgroundColor(hlColor);
					rbCurrent.setBackgroundColor(bgColor);
					rbCurrent = rbPosition;*/
					
					/*curTitleView.setVisibility(View.INVISIBLE);
					vPosition.setVisibility(View.VISIBLE);
					curTitleView = vPosition;*/
					
					break;
					
				case R.id.rb_more:
					
					this.mTabHost.setCurrentTabByTag("more");
					
//					mButtonList.get(mCurrentView).setBackgroundColor(bgColor);
					mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bar_top);
					mViews.get(mCurrentView).setVisibility(View.INVISIBLE);
					
					mCurrentView = 4;
					
//					mButtonList.get(mCurrentView).setBackgroundColor(hlColor);
					mButtonList.get(mCurrentView).setBackgroundResource(R.drawable.bg_hl);
					mViews.get(mCurrentView).setVisibility(View.VISIBLE);
					
					/*rbMore.setBackgroundColor(hlColor);
					rbCurrent.setBackgroundColor(bgColor);
					rbCurrent = rbMore;*/
					
					/*curTitleView.setVisibility(View.INVISIBLE);
					vMore.setVisibility(View.VISIBLE);
					curTitleView = vMore;*/
					
					break;
					
				default:
					break;
			}
		}
	}
	
	@Override
	public void onUserInteraction() {
		// TODO Auto-generated method stub
		super.onUserInteraction();
		
		Log.e(TAG, "onUserInteraction is called!");
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		
		Log.e(TAG, "dispatchTouchEvent is called!");

		if (mDetector.onTouchEvent(event)) {
			
			Log.e(TAG, "gesture consumes this event");
//			
			event.setAction(MotionEvent.ACTION_CANCEL);
//			
//			return true;
		}
//
		return super.dispatchTouchEvent(event);		// false
	}
	
	/*@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		
		Log.e(TAG, "onTouchEvent is called!");
		
		return super.onTouchEvent(event);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		
		Log.e(TAG, "onTouch is called!");
		
		if (mGA.mDetector.onTouchEvent(event)) {
			
			return true;
		}
		
		return false;
	}*/
	
}
